package com.xidian.yunwudemo.util;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtil {
    public static final TimeUtil TIME_UTIL = new TimeUtil();
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat SDF_SIMPLE = new SimpleDateFormat("yyyy-MM-dd");
    private TimeUtil(){
    }

    /**
     *
     * @param date 当前时刻时间
     * @return string 当前时刻一天前的时间
     */
    public String[] OneDay(Date date){
        String[] res = new String[2];
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        res[1] = SDF.format(date);
        res[0] = SDF.format(calendar.getTime());
        return res;
    }

    /**
     *
     * @param date
     * @return String[2],0:当前时间的0点，1：前一天
     */
    public String[] OneWeek(Date date){
        String[] res = new String[2];
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 获得当前时间0点
        calendar.add(Calendar.SECOND,0);
        calendar.add(Calendar.MINUTE,0);
        calendar.add(Calendar.HOUR_OF_DAY,0);
        calendar.add(Calendar.MILLISECOND,0);
        res[1] = SDF.format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH,-7);
        res[0] = SDF.format(calendar.getTime());
        return res;
    }

    /**
     *
     * @param date 当前时间
     * @return 返回一个len=7的String数组，日期从小到大排列,这样才能生成7个confidence，最后一天用不了（timeArray只要前7个）
     */
    public String[] BiologyWeekend(Date date){
        String[] week = new String[8];
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        for (int i = 0; i < 8; i++){
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH,-i);
            week[7-i] = SDF_SIMPLE.format(calendar.getTime());
        }
        return week;
    }
}
